package org.ljy.lotterysystem.dao.mapper;


import org.apache.ibatis.annotations.*;
import org.ljy.lotterysystem.dao.dataobject.ActivityUserDO;
import org.ljy.lotterysystem.dao.dataobject.UserDO;
import org.ljy.lotterysystem.service.enums.ActivityUserStatusEnum;

import java.util.List;

/**
 * 活动人员关联表
 */

@Mapper
public interface ActivityUserMapper {

    /**
     * 新增活动关联人员表
     * @param activityUserDOList
     * @return
     */
    @Insert("<script>" +
            " insert into activity_user (activity_id, user_id, user_name, status)" +
            " values <foreach collection = 'items' item='item' index='index' separator=','>" +
            " (#{item.activityId}, #{item.userId}, #{item.userName}, #{item.status})" +
            " </foreach>" +
            " </script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int batchInsert(@Param("items") List<ActivityUserDO> activityUserDOList);

    /**
     * 通过活动id查询关联用户
     * @param id
     * @return
     */
    @Select("select * from activity_user where activity_id = #{id}")
    List<ActivityUserDO> selectByActivityId(@Param("id") Long id);

    /**
     * 通过活动Id+人员id查询中奖人员
     * @param activityId
     * @param userIds
     * @return
     */

    @Select("<script>" +
            " select * from activity_user" +
            " where activity_id = #{activityId}" +
            " and user_id in" +
            " <foreach collection='userIds' item='userId' open='(' separator=',' close=')'>" +
            " #{userId}" +
            " </foreach>" +
            " </script>")
    List<ActivityUserDO> batchSelectByAUIds(@Param("activityId")Long activityId,
                                      @Param("userIds") List<Long> userIds);


    @Select("<script>" +
            " update activity_user set status = #{status}" +
            " where activity_id = #{activityId}" +
            " and user_id in" +
            " <foreach collection='userIds' item='userId' open='(' separator=',' close=')'>" +
            " #{userId}" +
            " </foreach>" +
            " </script>")
    void batchUpdateStatus(@Param("activityId")Long activityId,
                           @Param("userIds")List<Long> userIds,
                           @Param("status")ActivityUserStatusEnum status);
}
